USC_DIR = fullfile("..", "USC-HAD");

% 1. Walking Forward
% 2. Walking Left
% 3. Walking Right
% 4. Walking Upstairs
% 5. Walking Downstairs
% 6. Running Forward
% 7. Jumping Up
% 8. Sitting
% 9. Standing
% 10. Sleeping
% 11. Elevator Up
% 12. Elevator Down
label_names = dictionary([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [1, 1, 1, 2, 3, 6, 7, 4, 5, 10, 11, 12]);

train_X = [];
train_y = [];

for subject = 1:14
    for a = [1, 2, 3, 4, 5, 8, 9]
        for t = 1:5
            file_path = fullfile(USC_DIR, sprintf("Subject%d", subject), sprintf("a%dt%d.mat", a, t));
            if exist(file_path, "file")
                mat_data = load(file_path).sensor_readings;
                mat_data(:,4:6) = deg2rad(mat_data(:,4:6));
                % 100 Hz -> 50 Hz
                mat_data = mat_data(1:2:end, :);
				% 100 Hz -> 20 Hz
				% mat_data = mat_data(1:5:end, :);
                k = floor(size(mat_data, 1) / 128);
                data = reshape(mat_data(1:k*128, :, :), [128, k, 6]);
                data = permute(data, [2, 1, 3]);
                label = repmat(label_names(a), [k, 1]);
                train_X = cat(1, train_X, single(data));
                train_y = cat(1, train_y, uint8(label));
            end
        end
    end
end

save("USCHAD_Train.mat", "train_X", "train_y");
